function Promise(executor) {
    this.status = 'pending';
    this.value = null;
    this.reason = null;
    this.onFulfilledFunc = [];
    this.onRejectedFunc = [];

    const resolve = (value) => {
        if (value instanceof Promise) {
            return value.then(resolve. reject);
        }

        if (this.status === 'pending') {
            setTimeout(() => {
                this.value = value;
                this.status = 'fulfilled';

                this.onFulfilledFunc.forEach(func => {
                    func(value);
                })
            })
        }
    }

    const reject = (reason) => {
        if (this.status === 'pending') {
            setTimeout(() => {
                this.reason = reason;
                this.status = 'rejected';

                this.onRejectedFunc.forEach(func => {
                    func(reason);
                })
            })
        }
    }

    try {
        executor(resolve, reject);
    } catch (error) {
        reject(error);
    }
}

Promise.prototype.then = function(onfulfilled, onrejected) {
    onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data;
    onrejected = typeof onrejected === 'function' ? onrejected : error => { throw error };

    if (this.status === 'fulfilled') {
        onfulfilled(this.value);
    }
    if (this.status === 'rejected') {
        onrejected(this.reason);
    }
    if (this.status === 'pending') {
        this.onFulfilledFunc.push(onfulfilled);
        this.onRejectedFunc.push(onrejected);
    }
}

let promise = new Promise((resolve, reject) => {
    resolve('data')
});

promise.then(
    data => { console.log(1, data) },
    error => { console.log(error) }
)

console.log(1);